/*************
author:: libo
license:: MIT
*****************/
// 为防止出现指针错误，最好储存元素的id作为引用

#ifndef LIB_CELL_MESH_H
#define LIB_CELL_MESH_H
//#include<map>

//#include<tools_rbtree.h>
#include "Mesh_Frame.h"
#include "libcell_iteratorc.h"
#include<tools/intrbtree.h>
#define iterator_v lib_cell_iterator_vertices
#define iterator_f lib_cell_iterator_faces
#define iterator_c lib_cell_iterator_cells
#define iterator_edge lib_cell_iterator_edge

#define quote lib_cell_quote


/********************negative represent +inf*
************/
/**************return x>=y relationship
************/
#define RELATIONSHIP_PARTIAL_ORDER(x,y) (x<0?1:(y<0?0:(x>=y?1:-1)))
typedef struct Mesh{
    char* name;
    Int_RB_Tree *cells,*faces,*vertices,*halffaces;

	int cell_id,vertex_id,face_id,halfface_id;
    template_v * (*get_vertexp)(struct Mesh* ,int);
    template_hf* (*get_halffacep)(struct Mesh*,int);
    template_f* (*get_facep)(struct Mesh*,int);
    template_c* (*get_cellp)(struct Mesh*,int);


    template_v  (*get_vertex)(struct Mesh*,int);
    template_c (*get_cell)(struct Mesh*,int);
    template_f (*get_face)(struct Mesh*,int);
    template_hf(*get_halfface)(struct Mesh* ,int);
	template_f*(*get_facev)(struct Mesh*,template_v**,int);
	template_c*(*get_cellv)(struct Mesh*,template_v**,int);
	template_c* (*get_cellf)(struct Mesh*,template_f**,int);


	int (*num_v)(struct Mesh*);
	int (*num_c)(struct Mesh*);
	int (*num_f)(struct Mesh*);
    int (*num_hf)(struct Mesh*);
    
	template_v *(*create_vertex)(struct Mesh*);
    template_v* (*create_vertexv)(struct Mesh*,double*,int);
	template_c *(*create_cell)(struct Mesh*);
    template_f*(*create_face)(struct Mesh*);
    
    template_c *(*create_cellv)(struct Mesh*,template_v**,int);
    template_c *(*create_cellf)(struct Mesh*,template_hf**,int);
    template_f *(*create_facev)(struct Mesh*,template_v**,int);
    template_hf*(*create_halfface)(struct Mesh*,template_f*,template_v**,int);
//	void (*rearrange_id)(struct Mesh*)=mesh_rearrange_id;
    int (*delete_cell)(struct Mesh*,template_c,int);       
	int (*delete_vertex)(struct Mesh*,template_v,int); 
    int (*delete_face)(struct Mesh*,template_f,int);
    int (*delete_halfface)(struct Mesh* ,template_hf,int); 
    int (*vertex_is_boundary)(struct Mesh*,template_v);
    int (*face_is_boundary)(struct Mesh*,template_f);
    int (*cell_is_boundary)(struct Mesh* ,template_c);
    
    int (*face_is_in_cell)(template_f*f,template_c* c );
    int (*vertex_is_in_cell)(template_v* , template_c*);
    int (*vertex_is_in_face)(template_v*,template_f*); 

    Node* (*node_of_boundary_face)(struct Mesh*,template_f*);
    void (*external_cells_init_)(struct Mesh*);
    void (*external_cells_free_)(struct Mesh*);

    template_hf (*opposite_halfface)(template_hf);
	template_hf*(*s_opposite_halfface)(template_hf*);
   

    iterator_v (*v_begin)(struct Mesh*own);
    iterator_v (*v_end)(struct Mesh*own);

    iterator_hf (*hf_begin)(struct  Mesh*own);
    iterator_hf (*hf_end)(struct  Mesh*own);

    iterator_f (*f_begin)(struct  Mesh*own);
    iterator_f (*f_end)(struct  Mesh*own);

    iterator_c (*c_begin)(struct Mesh*own);
    iterator_c (*c_end)(struct Mesh*own);  
	
    iterator_v (*cv_begin)(struct Mesh*,template_c);
    iterator_v (*fv_begin)(struct Mesh*,template_f);
    iterator_v (*fv_end)(struct Mesh*,template_f);
	iterator_v (*hfv_begin)(struct Mesh*,template_hf);
	iterator_v (*hfv_end)(struct Mesh*,template_hf);
	iterator_v (*cv_end)(struct Mesh*,template_c);
    iterator_c (*vc_begin)(struct Mesh*,template_v);
    iterator_c (*vc_end)(struct Mesh*,template_v );
	iterator_f (*vf_begin)(struct Mesh*,template_v);
	iterator_f (*vf_end)(struct Mesh*,template_v);
    iterator_hf (*chf_begin)(struct Mesh*,template_c);
    iterator_hf (*chf_end)(struct Mesh*,template_c);
/**********
    ******************/
    Node* (*vv_begin)(struct Mesh*,template_v);
	Node* (*vv_end)(struct Mesh*,template_v);
    //以v为起点的边界遍历
    //判断成员f!=NULL作为终点条件
    //如果出现循环,成员变量f(在重复出现时)会自动变为NULL
    iterator_edge (*edge_begin)(struct Mesh* ,template_v,template_f);
	
	void (*reset_c_prop)(struct Mesh*,int);
	void (*reset_v_prop)(struct Mesh*,int);
    void (*reset_f_prop)(struct Mesh*,int);
	void (*reset_hf_prop)(struct Mesh*,int);
	
	Node* (*intersection_two_faces)(struct Mesh*,template_f*,template_f*);
	Node* (*non_manifold_vertices)(struct Mesh*);

    int (*is_manifold_vertices)(struct Mesh*,template_v*);
	
    void (*printself)(struct Mesh*);
	int simplex;
	void* prop;
	MeshT traits;

    int dimension;
    // Control delete operations and others
    int manifold_require;

    Node* external_cells;
        	
}Mesh;
#undef iterator_v 
#undef iterator_f
#undef iterator_c 
#undef iterator_edge 

#undef quote 

#endif
